# from nonebot import on_regex
# from nonebot.typing import T_State
# from nonebot.adapters.onebot.v11 import GroupMessageEvent, Bot, Message
# from nonebot.adapters.onebot.v11 import MessageSegment
# import os

# # 监听所有消息，检测是否包含"猫饼"
# CatBing = on_regex(pattern=r'(?i)猫饼', priority=1)

# @CatBing.handle()
# async def cat_bing_detect(bot: Bot, event: GroupMessageEvent, state: T_State):
#     # 获取当前工作目录并拼接图片路径
#     # image_path = os.getcwd() + '/src/plugins/maobing.png'
    
#     script_path = os.path.abspath(__file__)
#     print("abspath = ", script_path)
#     image_path = os.path.join(os.path.dirname(script_path),
#                             'maobing.png')
#     print("image path =", image_path)

#     # 使用 MessageSegment 来发送图片
#     image = MessageSegment.image(f"file://{image_path}")
#     await CatBing.finish(message=image)
    
from nonebot import on_regex
from nonebot.typing import T_State
from nonebot.adapters.onebot.v11 import GroupMessageEvent, Bot, Message
from nonebot.adapters.onebot.v11 import MessageSegment
import os

# 定义关键词和对应图片的映射
keyword_image_mapping = {
    r'(?i)猫包': 'maobao.jpg',
    r'(?i)猫饼': 'maobing.png',
    r'(?i)猫粒': 'maoli.png',
    r'(?i)猫片': 'maopian.png',
    r'(?i)猫塔': 'maota.png',
    r'(?i)猫柱': 'maozhu.png'
}

# 监听多个关键词的消息
CatImageResponder = on_regex(pattern="|".join(keyword_image_mapping.keys()), priority=1)

@CatImageResponder.handle()
async def respond_with_image(bot: Bot, event: GroupMessageEvent, state: T_State):
    # 获取脚本路径，用于定位图片
    script_path = os.path.abspath(__file__)
    script_dir = os.path.dirname(script_path)

    # 根据匹配的关键词找到对应图片
    for pattern, image_name in keyword_image_mapping.items():
        if event.get_plaintext().strip().lower().find(pattern.strip(r'(?i)')) != -1:
            # 拼接图片路径
            image_path = os.path.join(script_dir, image_name)

            # 发送图片
            image = MessageSegment.image(f"file://{image_path}")
            await CatImageResponder.finish(message=image)

